package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.entity.AboutUs;
import com.mdd.common.entity.Company;
import com.mdd.common.mapper.AboutUsMapper;
import com.mdd.common.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Feedback;
import com.mdd.common.entity.SystemHelp;
import com.mdd.common.mapper.FeedbackMapper;
import com.mdd.common.mapper.SystemHelpMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.front.service.ISystemService;
import com.mdd.front.validate.common.PageValidate;

@Service
public class SystemServiceImpl implements ISystemService {

	@Autowired
	private FeedbackMapper feedbackMapper;

	@Autowired
	private AboutUsMapper aboutUsMapper;

	@Autowired
	private CompanyMapper companyMapper;
	
	@Autowired
	private SystemHelpMapper systemHelpMapper;
	
	@Override
	public AjaxResult<Object> submitFeedback(Feedback feedback,Integer userId) {
		feedback.setUserId(userId);
		feedback.setCreateTime(System.currentTimeMillis() / 1000);
		feedbackMapper.insert(feedback);
		return AjaxResult.success();
	}

	@Override
	public AjaxResult<Object> addCooperation(Company company, Integer userId) {
		company.setCreateTime(System.currentTimeMillis() / 1000);
		companyMapper.insert(company);
		return AjaxResult.success();
	}

	@Override
	public AjaxResult<Object> selectAboutUs() {
		AboutUs aboutUs = aboutUsMapper.selectOne(new QueryWrapper<AboutUs>().last("limit 1"));
		return AjaxResult.success(aboutUs);
	}

	@Override
	public PageResult<SystemHelp> helpCenter(PageValidate pageValidate) {
		Integer pageNo = pageValidate.getPageNo();
		Integer pageSize = pageValidate.getPageSize();
		Page<SystemHelp> iPage = systemHelpMapper.selectPage(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<SystemHelp>().orderByDesc(SystemHelp::getCreateTime));
		iPage.getRecords().stream().forEach(user ->{
			user.setTimeShow(TimeUtils.timestampToDate(user.getCreateTime()));
		});
		return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
	}

	@Override
	public AjaxResult<Object> helpCenterBrowse(Integer id) {
		SystemHelp help = systemHelpMapper.selectById(id);
		if(null != help) {
			help.setBrowseNum(help.getBrowseNum()+1);
			systemHelpMapper.updateById(help);
		}
		return AjaxResult.success();
	}
}
